---
- name: Install helpful development packages
  dnf: name={{ item }} state=present
  with_items:
    - git
    - vim-enhanced

- name: Install external dependencies
  dnf: name={{ item }} state=present
  with_items:
    - npm
    - redis
    - fedmsg-hub
    - python-virtualenv
    - python3-flask-oidc

- name: Install Fedora Hubs development packages
  dnf: name={{ item }} state=present
  with_items:
    - gcc
    - gcc-c++
    - libffi-devel
    - openssl-devel
    - python-sphinx
    - python2-devel
    - python3-devel
    - redhat-rpm-config
    - sqlite-devel
  when: hubs_dev_mode

- name: Install the distribution versions of requirements.txt
  dnf: name={{ item }} state=present
  with_items:
    - python-alembic
    - python-arrow
    - python-bleach
    - python-decorator
    - python-dogpile-cache
    - python-fedmsg-core
    - python-fedmsg-meta-fedora-infrastructure
    - python-flask
    - python-flask-oidc
    - python-fmn-lib
    - python-fmn-rules
    - python-futures
    - python-html5lib
    - python-munch
    - pytz
    - python-sqlalchemy
    - python-markdown
    - python2-pkgwat-api
    - python-six
    - python-pygments
    - python-pygments-markdown-lexer
    - python-retask


# Add various helpful configuration files
- name: Install a custom bashrc
  template: src=bashrc dest=/home/{{ main_user }}/.bashrc
  when: hubs_dev_mode


# Create directory structure

- name: Create the directory structure
  file:
    path: "{{ item.path }}"
    state: directory
    owner: "{{ main_user }}"
    group: "{{ main_user }}"
    mode: "{{ item.mode }}"
    #setype: httpd_sys_content_rw_t
  with_items:
    - {path: "{{ hubs_base_dir }}", mode: 755}
    - {path: "{{ hubs_conf_dir }}", mode: 750}
    - {path: "{{ hubs_var_dir }}", mode: 750}


# Set up the Python development environment
- name: Install Fedora Hubs requirements.txt into hubs virtualenv
  become_user: "{{ main_user }}"
  pip:
    requirements: "{{ hubs_code_dir }}/requirements.txt"
    virtualenv: "{{ hubs_venv_dir}}"
    virtualenv_site_packages: yes

- name: Install Fedora Hubs test-requirements.txt into hubs virtualenv
  become_user: "{{ main_user }}"
  pip:
    requirements: "{{ hubs_code_dir }}/test-requirements.txt"
    virtualenv: "{{ hubs_venv_dir}}"
    virtualenv_site_packages: yes

- name: Install other packages into hubs virtualenv
  become_user: "{{ main_user }}"
  pip:
    name: "{{ item }}"
    virtualenv: "{{ hubs_venv_dir }}"
    virtualenv_site_packages: yes
  with_items:
    - bleach

- name: Install Fedora Hubs into the virtualenv
  become_user: "{{ main_user }}"
  command: "{{ hubs_venv_dir }}/bin/pip install -e {{ hubs_code_dir }}"
  args:
    creates: "{{ hubs_venv_dir }}/lib/python2.7/site-packages/fedora-hubs.egg-link"

- name: Set bin file context in the virtualenv
  become_user: "{{ main_user }}"
  file:
    path: "{{ hubs_venv_dir }}/bin"
    state: directory
    recurse: true
    setype: bin_t

- name: Add a basic Hubs configuration file
  template:
    src: "{{ item }}"
    dest: "{{ hubs_conf_dir }}/hubs_config.py"
  with_first_found:
    - hubs_config.{{ ansible_hostname }}
    - hubs_config
  become_user: "{{ main_user }}"
  notify: "hubs configuration change"

- name: Add a basic fedmsg configuration file
  template:
    src: "{{ item }}"
    dest: "/etc/fedmsg.d/hubs_config.py"
  with_first_found:
    - fedmsg_config.{{ ansible_hostname }}
    - fedmsg_config
  notify: "hubs configuration change"

- name: Configure application to authenticate with iddev.fedorainfracloud.org
  command:
    oidc-register
    --output-file {{ hubs_conf_dir }}/client_secrets.json
    https://iddev.fedorainfracloud.org/ {{ hubs_url }}
  become_user: "{{ main_user }}"
  args:
    creates: "{{ hubs_conf_dir }}/client_secrets.json"


# Set up, create, and populate the database.
- import_tasks: db-{{ hubs_db_type }}.yml


# Set up JavaScript requirements
- name: Install npm packages
  command: npm install
  become_user: "{{ main_user }}"
  args:
    creates: node_modules
    chdir: "{{ hubs_code_dir }}/hubs/static/client"

- name: Build JavaScript assests
  command: node_modules/.bin/webpack
  become_user: "{{ main_user }}"
  args:
    chdir: "{{ hubs_code_dir }}/hubs/static/client"
    creates: "{{ hubs_code_dir }}/hubs/static/js/build/common.js"


# Services
- name: Disable the system-wide fedmsg-hub
  service: name=fedmsg-hub state=stopped enabled=no

- name: Install the service files
  template:
    src: "{{ item }}.service"
    dest: /etc/systemd/system/{{ item }}.service
  with_items:
    - hubs-triage@
    - hubs-worker@
    - hubs-sse
    - hubs-fedmsg-hub
  register: service_installed

- name: reload systemd
  command: systemctl daemon-reload
  when: service_installed|changed

- name: Start and enable the services
  service: name={{ item }} state=started enabled=yes
  with_items:
    - redis
    - hubs-triage@1
    - hubs-triage@2
    - hubs-worker@1
    - hubs-worker@2
    - hubs-sse
    - hubs-fedmsg-hub


# Webserver
- import_tasks: webserver.yml
  when: not hubs_dev_mode
